//
//  MHCircleMemberViewModel.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/4/28.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHCircleMemberViewModel.h"
#import "MHCommonArrowItemViewModel.h"
#import "MHSetCircleInfoViewModel.h"
#import "MHBlackListViewModel.h"
#import "MHAuditListViewModel.h"

@implementation MHCircleMemberViewModel


- (instancetype)initWithServices:(id<MHViewModelServices>)services params:(NSDictionary *)params{
    if (self = [super initWithServices:services params:params]) {
        self.info = params[MHViewModelUtilKey];
        
    }
    return self;
}





-(void)initialize
{
    [super initialize];
    self.title = @"圈子成员";
    self.shouldPullUpToLoadMore = YES;
    /// 没有数据时，隐藏底部刷新控件
    self.shouldEndRefreshingWithNoMoreData = YES;
    self.style = UITableViewStyleGrouped;
    @weakify(self);
    
//    self.prefersNavigationBarBottomLineHidden = YES;
    self.setIdentityCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSDictionary  *dataDic ) {
    
        int memberId = [dataDic[@"memberId"] intValue];
         int identType = [dataDic[@"identType"] intValue];
        return [self.services.client setCircleMemberIdentType:memberId identType:identType];
    }];
    
    self.removeMemberCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSNumber  *identity ) {
        int memberId = [identity intValue];
        return [self.services.client deleteCircleMember: memberId];
    }];
    
    self.blakMemberCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSNumber  *identity ) {
        int memberId = [identity intValue];
        return [self.services.client pullCircleMemberBlack:self.info.autoId userId:memberId];
    }];
    
    self.cancelBlackMemberCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSNumber  *identity ) {
        int memberId = [identity intValue];
        return [self.services.client cancelCircleMemberBlack:memberId];
    }];
    
    self.cancelMemberTypeCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSNumber  *identity ) {
        int memberId = [identity intValue];
        return [self.services.client cancelCircleMemberIdentType:memberId] ;
    }];
 
    RAC(self, circleMembers) = self.requestRemoteDataCommand.executionSignals.switchToLatest;
    /// 数据源
    RAC(self,dataSource) = [RACObserve(self, circleMembers) map:^(NSArray * circleMembers) {
        @strongify(self)
        
        NSMutableArray *manages = [NSMutableArray array];
        NSMutableArray *members = [NSMutableArray array];
        for (MHCircleMember *member in circleMembers) {
            if (member.identType == 4) {
                [members addObject:member];
            }else
            {
                [manages addObject:member];
            }
        }
        MHCommonGroupViewModel *group0 = [MHCommonGroupViewModel groupViewModel];
        MHCommonArrowItemViewModel *auditViewModel = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"审批请求" icon:@"audit_requst"];
        auditViewModel.destViewModelClass = MHAuditListViewModel.class;
        auditViewModel.model = self.info;
        
        MHCommonArrowItemViewModel *circleMemberCell = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"黑名单" icon:@"circle_blacklist"];
        circleMemberCell.destViewModelClass = [MHBlackListViewModel class];
        circleMemberCell.model = self.info;
        
        
        
//        MHCommonArrowItemViewModel *yaoqing = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"邀请嘉宾"];
//        yaoqing.model = self.info;
        
        
        if (self.info.identType == MHCircleIdentityTypeOwner || self.info.identType == MHCircleIdentityTypeManager) {
            group0.itemViewModels = @[auditViewModel,circleMemberCell];
        }else
        {
//            group0.itemViewModels = @[circleMemberCell];
        }
        
        
        
        MHCommonGroupViewModel *group1 = [MHCommonGroupViewModel groupViewModel];
        group1.header = @"圈主、嘉宾、管理员";
        CGFloat limitWidth = MH_SCREEN_WIDTH - 2 * 20;
        CGFloat headerHeight1 = [group1.header mh_sizeWithFont:MHRegularFont_14 limitWidth:limitWidth].height + 5 *2;
        group1.headerHeight = headerHeight1;
        
        
        group1.itemViewModels = [self dataSourceWithcircleMembers:manages];
        MHCommonGroupViewModel *group2 = [MHCommonGroupViewModel groupViewModel];
        group2.header = @"成员";
        group2.headerHeight = MHTableViewFooterHeaderHeight(group2.header);
        group2.itemViewModels = [self dataSourceWithcircleMembers:members];
        

        if (members.count > 0) {
            if (self.info.identType == MHCircleIdentityTypeOwner || self.info.identType == MHCircleIdentityTypeManager)
            {
                self.dataSource = @[group0,group1,group2];
            }
            else
            {
                self.dataSource = @[group1,group2];
            }
        }else
        {
            
            if (self.info.identType == MHCircleIdentityTypeOwner || self.info.identType == MHCircleIdentityTypeManager)
            {
                self.dataSource = @[group0,group1];
            }else
            {
                 self.dataSource = @[group1];
            }
            
        }
        
        
        return self.dataSource;
    }];
    
    
    
    
    
    
    
}


- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page{
    
    NSMutableArray * (^mapcircleMembers)(NSArray *) = ^(NSArray *circleMembers) {
        if (page == 1) {
            /// 下拉刷新
        } else {
            /// 上拉加载
            circleMembers = @[(self.circleMembers ?: @[]).rac_sequence, circleMembers.rac_sequence].rac_sequence.flatten.array;
        }
        return [circleMembers mutableCopy];
    };
    
    return [[self.services.client fetchCircleMemberPagedListWithCircleId:self.info.autoId roleId:0 pageNum:page pageSize:self.perPage searchTxt:nil] map:mapcircleMembers];
}

#pragma mark - 辅助方法
- (NSArray *)dataSourceWithcircleMembers:(NSArray *)circleMembers {
    if (MHObjectIsNil(circleMembers) || circleMembers.count == 0) return nil;
    NSMutableArray *viewModels = [[circleMembers.rac_sequence map:^(MHCircleMember *circleMember) {
        
        MHCommonItemViewModel *viewModel  = [MHCommonItemViewModel itemViewModelWithTitle:circleMember.nickName   icon:circleMember.fullUserFace];
        __weak __typeof(self)weakSelf = self;
        viewModel.model = circleMember;
        switch (circleMember.identType) {
            case 1:
                viewModel.subtitle = @"圈主";
                break;
                
            case 2:
                viewModel.subtitle = @"管理员";
                break;
            case 3:
                viewModel.subtitle = @"嘉宾";
                break;
            default:
                viewModel.subtitle = @"成员";
                break;
        }
        return viewModel;
    }].array mutableCopy];

    return viewModels ?: @[] ;
}
@end
